//
//  VIPPlayController.swift
//  SeeFM_Swift
//
//  Created by CBCT_MBP on 2021/7/6.
//

import UIKit
import WebKit

class VIPPlayController: SFBaseController, WKNavigationDelegate, UIScrollViewDelegate {
    
    var webView: WKWebView?
    var url: String? = nil
    let apis = ["https://jx.618g.com/?url=",
                "https://www.kpezp.cn/jlexi.php?url=",
                "https://www.ckmov.vip/api.php?url=",
                "https://www.1717yun.com/jx/ty.php?url=",
                "https://jx.mw0.cc/?url=",
                "https://okjx.cc/?url="]
    
    init(url: String? = nil) {
        super.init(nibName: nil, bundle: nil)
        
        self.url = url
    }
    
    override func setup() {
        let config = WKWebViewConfiguration()
        let preference = WKPreferences()
        config.preferences = preference
        config.allowsInlineMediaPlayback = true

        let contentController = WKUserContentController()
        config.userContentController = contentController

        let webView = WKWebView(frame: .zero)
        webView.isOpaque = false
        webView.backgroundColor = UIColor.blue
        webView.allowsBackForwardNavigationGestures = true

        webView.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth, UIView.AutoresizingMask.flexibleHeight]
        webView.scrollView.delegate = self
        webView.navigationDelegate = self
        self.view.addSubview(webView)
        self.webView = webView
        webView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(Adapter.Screen.topHeight)
            make.height.equalTo(Adapter.Screen.width * 9 / 16)
        }
    }
    
    func playUrl(url: String, index: Int? = 0) -> String {
        let convertedUrl = String.init(format: "%@%@", apis[0],url)
        return convertedUrl
    }
    func url(url: String) -> URL? {
        let url = URL.init(string: url)
        return url
    }
    func play(url: URL?) {
        if let u = url {
            let req = URLRequest.init(url: u)
            webView?.load(req)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        guard let u = self.url else { return }
        play(url: url(url: playUrl(url: u)))
        view.backgroundColor = UIColor.white
        setupChangeAPIBtn()
        // Do any additional setup after loading the view.
    }
    
    
    func setupChangeAPIBtn() {
    
        let titles = ["解析1","解析2","解析3","解析4","解析5","解析6"]
        for i in 0..<apis.count {
            let btn = UIButton(type: .custom)
            btn.setTitle(titles[i], for: .normal)
            btn.setTitle(titles[i], for: .highlighted)
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.addTarget(self, action: #selector(analysisBtnAction(btn:)), for: .touchUpInside)
            btn.frame = CGRect.init(x: 50, y: 400 + i * 60 + 15*i, width: Int(Adapter.Screen.width) - 100, height: 60)
            btn.backgroundColor = kBlackColor_10
            btn.layer.cornerRadius = 30
            btn.layer.masksToBounds = true
            btn.tag = 1024 + i
            self.view.addSubview(btn)
        }
        
    }
    
    @objc func analysisBtnAction(btn: UIButton) {
        let tag = btn.tag - 1024
        
        guard let u = self.url else { return }
        play(url: url(url: playUrl(url: u, index: tag)))
    }
    
    override func backBtnAction() {
        if self.webView?.canGoBack == true {
            self.webView?.goBack()
        }else {
            super.backBtnAction()
        }
    }
}
